<template>
	<view>
		<goodlist :goods="goods" @clickGoodDetail="goGoodsDetail"></goodlist>
		<view class="isOver" v-if="flag">
			---没有更多数据---
		</view>
	</view>
</template>

<script>
	import goodlist from '../../components/goods-list/goods-list.vue'
	export default {
		data() {
			return {
				// 页码默认为1
				pageindex: 1,
				goods: [],
				// 触底标识
				flag: false
			}
		},
		components: {
			goodlist
		},
		onLoad() {
			this.getGoodList()
		},
		// 当页面触底时触发
		onReachBottom() {
			// 如果当前页面长度小于 pageindex * 10，就可以直接return不请求下一页
			if (this.goods.length < this.pageindex * 10) return this.flag = true
			this.pageindex++
			this.getGoodList()
		},
		onPullDownRefresh() {
			// 下拉刷新
			this.pageindex = 1
			this.goods = []
			this.flag = false
			setTimeout(() => {
				this.getGoodList(() => {
					uni.stopPullDownRefresh()
				})
			}, 1000)

		},
		methods: {
			// 获取商品列表的数据
			async getGoodList(callback) {
				const res = await this.$myRequest({
					url: '/getgoods',
					data: {
						pageindex: this.pageindex
					}
				})
				// 解构，每次请求之后之前的数据不删除
				this.goods = [...this.goods, ...res.data.message]
				// 如果传了回调函数clalback 就 调用
				callback && callback()
			},

			goGoodsDetail(id) {
				uni.navigateTo({
					url: '/pages/goods-detail/goods-detail?id=' + id
				})
				console.log(id)
			}
		}

	}
</script>

<style>
	.isOver {
		text-align: center;
		color: #C0C0C0;
	}
</style>
